"""
  # File       : checkPoint.py
  # Time       : 2021/12/13
  # Author     : XGT
  # version    : V1.0.0
  # Description: 实现关键字驱动和数据驱动联动,连接框架unittest
  # Note       : CheckPoint.checkAssert_raise()用于抛出异常，主要用于报告
                 因为return 与 raise 不能前后共用,故而提取出该独立方法
                 TestRunner可以在源码中自动修改报告名称和测试人等信息,
                 可实现自动发邮件功能send_email()
                 excel表格中的每一行做一个测试用例,包括文件名
"""

import unittest
import cProfile
from ddt import ddt, data
from Control import D_ExcelControl
from Log.logger import Log
from Base.Base import BaseOfWeb
from common import checkPoint
from unittestreport import TestRunner
from common import LogUtil as logu

test_data = D_ExcelControl.write_test_data()


@ddt
class Test(unittest.TestCase):

    @data(*test_data)
    @logu.logExceptionHandler(logu.exceptionHandler)
    def test_A(self,test_data):
        global wk
        global excel_path1
        if '.xlsx' in test_data:
            print("测试开始")
            excel_path1 = test_data
        else:
            if test_data['event'] == "open_browers":
                wk = BaseOfWeb(test_data['txt'])
                Log.info('现在执行关键字:{0}，结果:{1}'.format(test_data['event'], '正常'))
            elif 'assert' in test_data['event']:
                Res = getattr(wk, test_data['event'])(**test_data)
                if checkPoint.CheckPoint().checkAssertEqual(Res, test_data['except']):
                    staut = 'OK'
                    D_ExcelControl.write_Test_Result(excel_path1, Res, test_data['col']-2, staut)
                    Log.info('现在执行关键字:{0}，结果:{1}'.format(test_data['event'], '断言成功'))
                    Log.info('断言成功！实际结果为:{0}={1}'.format(Res, test_data['except']) + '\n')
                else:
                    staut = 'Failed'
                    D_ExcelControl.write_Test_Result(excel_path1, Res, test_data['col']-2, staut)
                    Log.error('现在执行关键字:{0}，结果:{1}!,{2}{3}错误位置位于{4}文件的第{5}行，出现错误。{6}{7}操作描述为:{8},实际结果为:{9}!={10}{11}'
                              .format(test_data['event'], '断言失败', '\n','                                                                ',
                               excel_path1,str(test_data['col']-1), '\n',
                               '                                                                ',
                                test_data['Des'], Res, test_data['except'], '\n'))
                    checkPoint.checkAssert_raise(Res, test_data['except'])
            elif 'click' in test_data['event']:
                getattr(wk, test_data['event'])(**test_data)
                Log.info('现在执行关键字:{0}，操作描述:{1}，执行结果:{2}'.format(test_data['event'], test_data['Des'], '正常'))
            else:
                getattr(wk, test_data['event'])(**test_data)

def suite():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    suite.addTest(loader.loadTestsFromTestCase(Test))
    return suite


if __name__ == '__main__':

    br = TestRunner(suite())
    br.run()
    cProfile.run('suite')